package com.zmn.plat.business.impl.message;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.utils.http.HttpUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.message.MsgWechatTemplateBService;
import com.zmn.plat.model.vo.message.MsgWechatAppsVO;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：模板消息配置微信模板消息业务接口实现类
 *
 * @author wangjie
 * @since 2020/6/22 14:31
 **/
@Slf4j
@Service
public class MsgWechatTemplateBServiceImpl implements MsgWechatTemplateBService {

    @Value("${plat.template.wechat.appid.url}")
    private String wechatAppIdUrl;

    @Override
    public List<MsgWechatAppsVO> listWeChatAppIdAndNames() {

        String requestParm = "{\n" + "  \"body\": {\n" + "    \"plat\": null,\n" + "    \"type\": 21\n" + "  },\n" + "  \"debugTag\": \"\"\n" + "}";
        String post = HttpUtil.post(wechatAppIdUrl, requestParm);
        if (StringUtil.isBlank(post)) {
            return Collections.emptyList();
        }
        Map map = JSONObject.parseObject(post, Map.class);
        String dataKey = "data";
        Object data = map.get(dataKey);
        if (Objects.isNull(data)) {
            return Collections.emptyList();
        }
        List<MsgWechatAppsVO> result = JSONObject.parseArray(JSONObject.toJSONString(data), MsgWechatAppsVO.class);
        return result;
    }
}
